<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link href="lay/layui/css/layui.css" media="all" rel="stylesheet">
    <script charset="utf-8" src="lay/layui/layui.js"></script>
    <script src="js/jquery-3.3.1.min.js" type="text/javascript"></script>
</head>
<body class="layui-layout-body">
<div class="layui-container" style="margin-top: 50px;width: 990px;">
    <form action="" class="layui-form" method="POST">
        <div class="layui-form-item" style="margin-left: 50px;">
            <label class="layui-form-label">条件</label>
            <div class="layui-inline" style="width: 200px;">
                <input class="layui-input" name="index" placeholder="请输入试卷名称" type="text">
            </div>
            <div class="layui-inline" style="width: 200px;">
                <button class="layui-btn" lay-filter="find" lay-submit>搜索</button>
            </div>
        </div>

    </form>
    <table class="layui-table" id="test" lay-filter="test"></table>
    <script id="barDemo1" type="text/html">
        <a class="layui-btn layui-btn-xs" lay-event="addTime">延时</a>
    </script>
    <script id="barDemo2" type="text/html">
        <a class="layui-btn layui-btn-xs" lay-event="stu-manage">考生列表</a>
    </script>
    <script id="barDemo3" type="text/html">
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="addStu">添加考生</a>
    </script>
    <script id="barDemo4" type="text/html">
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="addTea">添加教师</a>
    </script>
    <script charset="utf-8" src="layui/layui.js"></script>
    <script>
        layui.use(['form', 'table'], function () {
            var form = layui.form;
            var table = layui.table;
            form.on('submit(find)', function (data) {
                //获取 class_id 和 course_nam
                var index = data.field.index;
                var teacher = sessionStorage.getItem("id");
                if (index.length == 0) {
                    index = "null";
                }
                table.reload('test', {
                    url: '/question/test/findMeCreatByIndex'
                    , where: {
                        index: index,
                        teacherId: teacher
                    } //设定异步数据接口的额外参数
                    //,height: 300
                });
                return false;
            });
        });
        layui.use(['table', 'transfer', 'layer', 'util', 'form', 'laydate'], function () {
            var table = layui.table;
            var transfer = layui.transfer;
            var layer = layui.layer;
            var util = layui.util;
            var form = layui.form;
            var laydate = layui.laydate;
            table.render({
                elem: '#test'
                , url: '/question/test/getMeCreat/' + sessionStorage.getItem("id")
                , title: '学生列表'
                , cols: [
                    [
                        {field: 'test_id', width: 100, title: '试卷编号', sort: true}
                        , {field: 'test_name', width: 100, title: '试卷名称'}
                        , {field: 'test_begin_time', width: 180, title: '开始时间'}
                        , {field: 'test_end_time', width: 180, title: '结束时间'}
                        , {fixed: 'right', title: '延时', toolbar: '#barDemo1', width: 90}
                        , {fixed: 'right', title: '学生管理', toolbar: '#barDemo2', width: 120}
                        , {fixed: 'right', title: '添加考生', toolbar: '#barDemo3', width: 90}
                        , {fixed: 'right', title: '改卷教师', toolbar: '#barDemo4', width: 90}
                    ]
                ]
                , page: false
            });

            //监听行工具事件
            table.on('tool(test)', function (obj) {
                var data = obj.data;
                var oldTime = data.endTime;
                if (obj.event === 'addTime') {
                    var time;
                    var index = layer.open({
                        type: 1,
                        title: '延时',
                        area: ['350px', '200px'],
                        skin: 'layer-extskin-overauto',
                        content: $('#popUpdate').html(),
                        success: function () {
                            laydate.render({
                                elem: '#endTime',
                                trigger: 'click',
                                type: 'datetime'
                            });

                            $("#submit").click(function () {
                                time = $("#endTime").val();
                                if (time.length == 0) {
                                    alert("结束时间不得为空");
                                    return;
                                }

                                if (new Date(oldTime).getTime() - new Date(time).getTime() > 0) {
                                    alert("修改后时间不得早于之前的结束时间，请检查");
                                    return;
                                }
                                console.log(time);
                                $.ajax({
                                    url: '/question/test/editEndTime/' + data.test_id, //请求url
                                    data: JSON.stringify({"time": time}), //请求参数，类似于username=xxx&password=xxx
                                    type: 'put', //请求方式
                                    dataType: 'json', //返回值的类型
                                    contentType: 'application/json;charset=utf-8',
                                    success: function (result) {  //服务器成功返回时执行的函数
                                        console.log(JSON.stringify(result));
                                        if (result.code == 2000) {
                                            alert("修改成功！");
                                            layer.close(index);
                                            window.location.href = "me_creat.html";

                                        } else {
                                            alert("修改失败!\n" + result.msg);
                                            return;
                                        }
                                    },
                                    error: function () {
                                        alert("修改失败，网络异常！");
                                        return;
                                    }
                                });
                            });
                        }
                    });

                }
                if (obj.event === 'addStu') {
                    var list = [];
                    $.ajax({
                        async: false,
                        url: '/question/test/findStu/' + sessionStorage.getItem("course") + '/' + data.test_id, //请求url
                        data: '', //请求参数，类似于username=xxx&password=xxx
                        type: 'get', //请求方式
                        dataType: 'json', //返回值的类型
                        contentType: 'application/json;charset=utf-8',
                        success: function (result) {  //服务器成功返回时执行的函数
                            console.log(JSON.stringify(result));
                            if (result.code == 2000) {
                                console.log(result.list);
                                list = result.list;
                            } else {
                                alert("获取学生列表失败！");
                            }
                        },
                        error: function () {
                            alert("网络错误，请稍后重试！")
                        }
                    });
                    var index = layer.open({
                        type: 1,
                        title: '添加考生',
                        area: ['600px', '550px'],
                        skin: 'layer-extskin-overauto',
                        content: $('#addStu').html(),
                        success: function () {
                            transfer.render({
                                elem: '#add-stu'
                                , data: list
                                , title: ['待添加考生信息', '已添加考生信息']
                                , showSearch: true
                                , width: 250
                                , id: 'add'
                            });
                            $("#addToTest").click(function () {
                                var studentList = transfer.getData('add');
                                console.log(studentList);
                                $.ajax({
                                    url: '/question/test/addStudentToTest/' + data.test_id, //请求url
                                    data: JSON.stringify({"studentList": studentList}), //请求参数，类似于username=xxx&password=xxx
                                    type: 'post', //请求方式
                                    dataType: 'json', //返回值的类型
                                    contentType: 'application/json;charset=utf-8',
                                    success: function (result) {  //服务器成功返回时执行的函数
                                        console.log(JSON.stringify(result));
                                        if (result.code == 2000) {
                                            alert("添加成功！");
                                            layer.close(index);
                                        } else {
                                            alert("添加失败!\n" + result.msg);
                                        }
                                    },
                                    error: function () {
                                        alert("添加失败，网络异常！");
                                    }
                                });
                            });

                        }
                    });

                }
                if (obj.event === 'stu-manage') {
                    console.log(data.test_id);
                    sessionStorage.setItem("testId", data.test_id);
                    window.location.href = "/test_stu_manage.html";
                }
                if (obj.event === 'addTea') {
                    var list1 = [];
                    $.ajax({
                        async: false,
                        url: '/question/test/findTea/' + sessionStorage.getItem("course") + '/' + data.test_id, //请求url
                        data: '', //请求参数，类似于username=xxx&password=xxx
                        type: 'get', //请求方式
                        dataType: 'json', //返回值的类型
                        contentType: 'application/json;charset=utf-8',
                        success: function (result) {  //服务器成功返回时执行的函数
                            console.log(JSON.stringify(result));
                            if (result.code == 2000) {
                                console.log(result.list);
                                list1 = result.list;
                            } else {
                                alert("获取学生列表失败！");
                            }
                        },
                        error: function () {
                            alert("网络错误，请稍后重试！")
                        }
                    });
                    var index1 = layer.open({
                        type: 1,
                        title: '添加教师',
                        area: ['600px', '550px'],
                        skin: 'layer-extskin-overauto',
                        content: $('#addTea').html(),
                        success: function () {
                            transfer.render({
                                elem: '#add-tea'
                                , data: list1
                                , title: ['所有教师信息', '待添加教师信息']
                                , showSearch: true
                                , width: 250
                                , id: 'tea'
                            });
                            $("#addForTest").click(function () {
                                var teacherList = transfer.getData('tea');
                                console.log(teacherList);
                                $.ajax({
                                    url: '/question/test/addTeacherToTest/' + data.test_id, //请求url
                                    data: JSON.stringify({"teacherList": teacherList}), //请求参数，类似于username=xxx&password=xxx
                                    type: 'post', //请求方式
                                    dataType: 'json', //返回值的类型
                                    contentType: 'application/json;charset=utf-8',
                                    success: function (result) {  //服务器成功返回时执行的函数
                                        console.log(JSON.stringify(result));
                                        if (result.code == 2000) {
                                            alert("添加成功！");
                                            layer.close(index1);
                                        } else {
                                            alert("添加失败!\n" + result.msg);
                                        }
                                    },
                                    error: function () {
                                        alert("添加失败，网络异常！");
                                    }
                                });
                            });

                        }
                    });

                }
            });
        });
    </script>

</div>
<script id="popUpdate" type="text/html">
    <div class="layui-form-mid">
        <label class="layui-form-label">结束时间：</label>
        <div class="layui-input-inline">
            <input type="text" class="layui-input" name="endTime" id="endTime"
                   placeholder="yyyy-MM-dd HH:mm:ss">
        </div>

    </div>
    <div class="layui-form-mid ">
        <button class="layui-btn" id="submit" style="margin-left: 160px;">确认</button>
    </div>

</script>
<script id="addStu" type="text/html">
    <div class="layui-form-mid">
        <fieldset class="layui-elem-field layui-field-title">
            <legend>考生信息列表</legend>
        </fieldset>
        <div id="add-stu" class="demo-transfer"></div>

    </div>
    <div class="layui-form-mid">
        <button class="layui-btn" id="addToTest" style="margin-left: 160px;">确认</button>
    </div>
</script>
<script id="addTea" type="text/html">
    <div class="layui-form-mid">
        <fieldset class="layui-elem-field layui-field-title">
            <legend>教师信息列表</legend>
        </fieldset>
        <div id="add-tea" class="demo-transfer"></div>

    </div>
    <div class="layui-form-mid">
        <button class="layui-btn" id="addForTest" style="margin-left: 160px;">确认</button>
    </div>
</script>
<script>
    //二级菜单联动
    layui.use('element', function () {
        var element = layui.element;

    });
</script>
</body>
</html>